package com.zhenzhen.xszhcp.controller;

import com.zhenzhen.xszhcp.model.*;
import com.zhenzhen.xszhcp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class delController {
    @Autowired
    private LogService logService;
    @Autowired
    private GroupinfoService groupinfoService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private KechengService kechengService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private SemesterService semesterService;
    @Autowired
    private ZhuangtaiService zhuangtaiService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private ZhcjService zhcjService;
    @Autowired
    private KcblService kcblService;

    /*删除用户组信息*/
    @RequestMapping("/delyhzxxgl")
    public boolean delyhzxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Map paramap=new HashMap();
        paramap.put("groupid",id);
        if(usersService.getuserList(paramap).size()!=0)
            return false;
        if (groupinfoService.delGroupinfo(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的用户组信息",logService);
            return true;
        }
        return false;
    }

    /*删除课程*/
    @RequestMapping("/delkcxxgl")
    public boolean delkcxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Map paramMap = new HashMap();
        paramMap.put("sskid",id);
        List<Grade> grades=gradeService.getGradeList(paramMap);
        if (grades.size()!=0)
            return false;
        if (kechengService.delKecheng(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的课程信息",logService);
            return true;
        }
        return false;
    }

    /*删除状态信息*/
    @RequestMapping("/delztxxgl")
    public boolean delztxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Map paramMap = new HashMap();
        paramMap.put("ztid",id);
        if (zhcjService.getZhcjList(paramMap).size()!=0)
            return false;
        if (zhuangtaiService.delZhuangtai(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的状态信息",logService);
            return true;
        }
        return false;
    }

    /*删除用户账号*/
    @RequestMapping("/delyhzhgl")
    public boolean delyhzhgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        if (usersService.deleteByPrimaryKey(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的用户",logService);
            return true;
        }
        return false;
    }

    /*删除学期信息*/
    @RequestMapping("/delxqxxgl")
    public boolean delxqxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Map paramMap = new HashMap();
        paramMap.put("sssid",id);
        if (gradeService.getGradeList(paramMap).size()!=0)
            return false;
        if (semesterService.delSemester(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学期信息",logService);
            return true;
        }
        return false;
    }

    /*删除活动记录*/
    @RequestMapping("/delhdjlgl")
    public boolean delhdjlgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        if (activityService.delActivity(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的活动记录信息",logService);
            return true;
        }
        return false;
    }

    /*教师删除学生成绩*/
    @RequestMapping("/delxscjgl")
    public boolean delxscjgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Grade grade = gradeService.getGradeById(id);
        List<Kcbl> kcbls = kcblService.getkcbllist();
        double dybl = 0.3;
        double tybl = 0.1;
        double stbl = 1;
        double zhbl = 0.6;
        DecimalFormat df = new DecimalFormat("#.00");
        if (kcbls!=null){
            for(Kcbl kcbl:kcbls){
                String bl = kcbl.getBl();
                String name = kcbl.getKname();
                if (name.equals("德育")){
                    dybl = Double.parseDouble(bl);
                }
                if (name.equals("体育")){
                    tybl = Double.parseDouble(bl);
                }
                if (name.equals("素拓")){
                    stbl = Double.parseDouble(bl);
                }
                if (name.equals("综合成绩学分绩点")){
                    zhbl = Double.parseDouble(bl);
                }
            }
        }
        if (gradeService.delGrade(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的成绩信息",logService);
            int sskid = grade.getSskid();
            int st = grade.getSt();
            String gradestr = grade.getGrade();
            int sssid = grade.getSssid();
            String xfjd = grade.getXfjd();
            Map sinfomap = new HashMap();
            sinfomap.put("ssuid",st);
            sinfomap.put("st",st);
            sinfomap.put("sssid",sssid);
            Zhcj zhcj = zhcjService.getZhcjByuands(sinfomap);
            List<Grade> grades = gradeService.getGradeList(sinfomap);
            if (zhcj!=null){
                Kecheng kecheng = kechengService.getKechengById(sskid);
                int xf = kecheng.getXf();
                String kname = kecheng.getKname();
                if (kname.equals("素拓"))
                    zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) - Double.parseDouble(gradestr) * stbl));
                else if (kname.equals("德育")) {
                    zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) - Double.parseDouble(gradestr) * dybl));
                    zhcj.setYscj(String.valueOf(Double.parseDouble(zhcj.getYscj()) - Double.parseDouble(gradestr) * dybl));
                } else if (kname.equals("体育")) {
                    zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) - Double.parseDouble(gradestr) * tybl));
                    zhcj.setYscj(String.valueOf(Double.parseDouble(zhcj.getYscj()) - Double.parseDouble(gradestr) * tybl));
                } else {
                        double stcj=0;
                        double dycj=0;
                        double tycj=0;
                        double zcj=0;
                        double zxf=0;
                        int dkxf=0;
                        if (grades!=null){
                            for (Grade grade2:grades){
                                int kid = grade2.getSskid();
                                Kecheng kecheng2 = kechengService.getKechengById(kid);
                                String knamestr = "已删除该课程";
                                if (kecheng2!=null) {
                                    knamestr = kecheng2.getKname();
                                    dkxf = kecheng2.getXf();
                                }
                                if (knamestr.equals("素拓")){
                                    stcj = Double.parseDouble(grade2.getGrade());
                                }
                                if (knamestr.equals("德育")){
                                    dycj = Double.parseDouble(grade2.getGrade());
                                }
                                if (knamestr.equals("体育")){
                                    tycj = Double.parseDouble(grade2.getGrade());
                                }
                                if (knamestr.equals("已删除该课程")){
                                    continue;
                                }
                                else {
                                    zcj=zcj+Double.parseDouble(grade2.getGrade())*dkxf;
                                    zxf=zxf+dkxf;
                                }
                            }
                        }
                        double zxfjd=0;
                        if (zxf!=0)
                            zxfjd=zcj/zxf;
                        zhcj.setZhcj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl+stcj));
                        zhcj.setYscj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl));
                        zhcj.setZxfjd(df.format(zxfjd));
                    }
                }
                if (zhcjService.upZhcj(zhcj) != 0)
                    setLog.setlod(httpServletRequest, "学生" + st + "的综合成绩已更新", logService);
                 return true;
        }
        return false;
    }

    /*删除班级信息*/
    @RequestMapping("/delbjxxgl")
    public boolean delbjxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Map paramMap = new HashMap();
        paramMap.put("sscid",id);
        if (studentService.getStudentList(paramMap).size()!=0)
            return false;
        if (classesService.delClasses(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的班级信息",logService);
            return true;
        }
        return false;
    }

    /*教师管理员删除学生*/
    @RequestMapping("/delxsxxgl")
    public boolean delxsxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        //删除学生活动
        if (activityService.delActivitybyStudent(id)>0){
             setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学生全部活动信息",logService);
        }
        //删除学生信息
        Student student = studentService.getStudentById(id);
        Users users = usersService.getusersByusername(String.valueOf(student.getXuehao()));
        if (studentService.delStudent(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学生",logService);
            //删除学生账号
            if (usersService.deleteByPrimaryKey(users.getId())!=0){
                setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学生账号",logService);
            }
            //删除学生日志信息
            if (logService.delLogbyStudent(users.getId())>0){
                setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学生全部日志信息",logService);
            }
            //删除学生综合成绩
            List<Zhcj> zhcjs = zhcjService.getZhcjByUid(id);
            if (zhcjs!=null){
            for (Zhcj zhcj : zhcjs){
                zhcjService.delZhcj(zhcj.getId());
                }
                setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学生综合成绩信息",logService);
            }
            //删除学生全部成绩信息
            if (gradeService.deleteBystudent(id)!=0){
                setLog.setlod(httpServletRequest, "删除了主键为"+id+"的学生全部成绩信息",logService);
            }
            return true;
        }
        return false;
    }

    /*删除菜单*/
    @RequestMapping("/delcdgl")
    public boolean delcdgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        if (menuService.deleteByPrimaryKey(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的菜单",logService);
            return true;
        }
        return false;
    }

    /*删除教师信息*/
    @RequestMapping("/deljsxxgl")
    public boolean deljsxxgl(HttpServletRequest httpServletRequest, @RequestParam int id){
        Teacher teacher = teacherService.getTeacherById(id);
        Map paramMap = new HashMap();
        paramMap.put("teacher",id);
        if (kechengService.getKechengListteacher(paramMap).size()!=0)
            return false;
        if (teacherService.delTeacher(id)!=0) {
            setLog.setlod(httpServletRequest, "删除了主键为"+id+"的教师",logService);
            Users users = usersService.getusersByusername(String.valueOf(teacher.getTcard()));
            if (usersService.deleteByPrimaryKey(users.getId())!=0)
                setLog.setlod(httpServletRequest, "删除了主键为"+id+"的教师账号",logService);
            return true;
        }
    return false;
    }
}

